var t = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(t) {
    return typeof t;
} : function(t) {
    return t && "function" == typeof Symbol && t.constructor === Symbol && t !== Symbol.prototype ? "symbol" : typeof t;
};

!function(e, o) {
    "object" === ("undefined" == typeof exports ? "undefined" : t(exports)) && "undefined" != typeof module ? module.exports = o() : "function" == typeof define && define.amd ? define(o) : e.WeCropper = o();
}(void 0, function() {
    function e(t) {
        return "function" == typeof t;
    }
    function o(t) {
        return t.charAt(0).toUpperCase() + t.slice(1);
    }
    function n(t) {
        for (var e = [], o = arguments.length - 1; o-- > 0; ) e[o] = arguments[o + 1];
        u.forEach(function(o, n) {
            void 0 !== e[n] && (t[o] = e[n]);
        });
    }
    function i(t, e) {
        Object.defineProperties(t, e);
    }
    function r(t, e, o) {
        var n = null, i = null;
        return function() {
            var r = +new Date();
            i || (i = r), r - i > o ? (n && clearTimeout(n), i = r, t.apply(this, arguments)) : (n && clearTimeout(n), 
            n = setTimeout(function() {
                t.apply(this, arguments);
            }, e));
        };
    }
    function a() {
        return c || (c = wx.getSystemInfoSync()), c;
    }
    var c = void 0, u = [ "touchstarted", "touchmoved", "touchended" ], d = {}, h = {
        id: {
            default: "cropper",
            get: function() {
                return d.id;
            },
            set: function(t) {
                "string" != typeof t && console.error("id：" + t + " is invalid"), d.id = t;
            }
        },
        width: {
            default: 750,
            get: function() {
                return d.width;
            },
            set: function(t) {
                "number" != typeof t && console.error("width：" + t + " is invalid"), d.width = t;
            }
        },
        height: {
            default: 750,
            get: function() {
                return d.height;
            },
            set: function(t) {
                "number" != typeof t && console.error("height：" + t + " is invalid"), d.height = t;
            }
        },
        scale: {
            default: 2.5,
            get: function() {
                return d.scale;
            },
            set: function(t) {
                "number" != typeof t && console.error("scale：" + t + " is invalid"), d.scale = t;
            }
        },
        zoom: {
            default: 5,
            get: function() {
                return d.zoom;
            },
            set: function(t) {
                "number" != typeof t ? console.error("zoom：" + t + " is invalid") : (t < 0 || t > 10) && console.error("zoom should be ranged in 0 ~ 10"), 
                d.zoom = t;
            }
        },
        src: {
            default: "cropper",
            get: function() {
                return d.src;
            },
            set: function(t) {
                "string" != typeof t && console.error("id：" + t + " is invalid"), d.src = t;
            }
        },
        cut: {
            default: {},
            get: function() {
                return d.cut;
            },
            set: function(e) {
                "object" !== (void 0 === e ? "undefined" : t(e)) && console.error("id：" + e + " is invalid"), 
                d.cut = e;
            }
        },
        onReady: {
            default: null,
            get: function() {
                return d.ready;
            },
            set: function(t) {
                d.ready = t;
            }
        },
        onBeforeImageLoad: {
            default: null,
            get: function() {
                return d.beforeImageLoad;
            },
            set: function(t) {
                d.beforeImageLoad = t;
            }
        },
        onImageLoad: {
            default: null,
            get: function() {
                return d.imageLoad;
            },
            set: function(t) {
                d.imageLoad = t;
            }
        },
        onBeforeDraw: {
            default: null,
            get: function() {
                return d.beforeDraw;
            },
            set: function(t) {
                d.beforeDraw = t;
            }
        }
    }, s = function(t, e, o, n, i) {
        var r, a, c;
        return r = Math.round(i.x - n.x), a = Math.round(i.y - n.y), c = Math.round(Math.sqrt(r * r + a * a)), 
        t + .001 * o * (c - e);
    }, l = {
        touchStart: function(t) {
            var e = this, o = t.touches, i = o[0], r = o[1];
            n(e, !0, null, null), e.__oneTouchStart(i), t.touches.length >= 2 && e.__twoTouchStart(i, r);
        },
        touchMove: function(t) {
            var e = this, o = t.touches, i = o[0], r = o[1];
            n(e, null, !0), 1 === t.touches.length && e.__oneTouchMove(i), t.touches.length >= 2 && e.__twoTouchMove(i, r);
        },
        touchEnd: function(t) {
            var e = this;
            n(e, !1, !1, !0), e.__xtouchEnd();
        }
    }, f = function(t, e) {
        var o = this, n = {};
        return i(o, h), Object.keys(h).forEach(function(t) {
            n[t] = h[t].default;
        }), Object.assign(o, n, t), o.inWitchComponent = e, o.prepare(), o.attachPage(), 
        o.createCtx(), o.observer(), o.cutt(), o.methods(), o.init(), o.update(), o;
    };
    return f.prototype.init = function() {
        var t = this, e = t.src;
        return t.version = "1.2.0", "function" == typeof t.onReady && t.onReady(t.ctx, t), 
        e && t.pushOrign(e), n(t, !1, !1, !1), t.oldScale = 1, t.newScale = 1, t;
    }, Object.assign(f.prototype, l), f.prototype.prepare = function() {
        var t = this, e = a().windowWidth;
        t.attachPage = function() {
            var e = getCurrentPages();
            e[e.length - 1].wecropper = t;
        }, t.createCtx = function() {
            var e = t.id;
            e ? t.ctx = wx.createCanvasContext(e, t.inWitchComponent) : console.error("constructor: create canvas context failed, 'id' must be valuable");
        }, t.deviceRadio = e / 750;
    }, f.prototype.observer = function() {
        var t = this, e = [ "ready", "beforeImageLoad", "beforeDraw", "imageLoad" ];
        t.on = function(n, i) {
            return e.indexOf(n) > -1 ? "function" == typeof i && ("ready" === n ? i(t) : t["on" + o(n)] = i) : console.error("event: " + n + " is invalid"), 
            t;
        };
    }, f.prototype.methods = function() {
        var t = this, o = t.id, n = t.deviceRadio, i = t.width, a = t.height, c = t.cut, u = c.x;
        void 0 === u && (u = 0);
        var d = c.y;
        void 0 === d && (d = 0);
        var h = c.width;
        void 0 === h && (h = i);
        var s = c.height;
        void 0 === s && (s = a), t.updateCanvas = r(function() {
            return t.croperTarget && t.ctx.drawImage(t.croperTarget, t.imgLeft, t.imgTop, t.scaleWidth, t.scaleHeight), 
            e(t.onBeforeDraw) && t.onBeforeDraw(t.ctx, t), t.setBoundStyle(c), t.ctx.draw(), 
            t;
        }, 0, 0), t.pushOrign = function(o) {
            return t.src = o, e(t.onBeforeImageLoad) && t.onBeforeImageLoad(t.ctx, t), wx.getImageInfo({
                src: o,
                success: function(o) {
                    var n = o.width / o.height;
                    t.croperTarget = o.path, n < h / s ? (t.rectX = u, t.baseWidth = h, t.baseHeight = h / n, 
                    t.rectY = d - Math.abs((s - t.baseHeight) / 2)) : (t.rectY = d, t.baseWidth = s * n, 
                    t.baseHeight = s, t.rectX = u - Math.abs((h - t.baseWidth) / 2)), t.imgLeft = t.rectX, 
                    t.imgTop = t.rectY, t.scaleWidth = t.baseWidth, t.scaleHeight = t.baseHeight, t.updateCanvas(), 
                    e(t.onImageLoad) && t.onImageLoad(t.ctx, t);
                }
            }), t.update(), t;
        }, t.getCropperImage = function() {
            for (var i = [], r = arguments.length; r--; ) i[r] = arguments[r];
            var a = toString.call(i[0]), c = i[i.length - 1];
            switch (a) {
              case "[object Object]":
                var l = i[0].quality;
                void 0 === l && (l = 10), "number" != typeof l ? console.error("quality：" + l + " is invalid") : (l < 0 || l > 10) && console.error("quality should be ranged in 0 ~ 10"), 
                wx.canvasToTempFilePath({
                    canvasId: o,
                    x: u + 1,
                    y: d + 1,
                    width: h - 2,
                    height: s - 2,
                    destWidth: (h - 2) * l / (10 * n),
                    destHeight: (s - 2) * l / (10 * n),
                    success: function(o) {
                        e(c) && c.call(t, o.tempFilePath);
                    },
                    fail: function(o) {
                        e(c) && c.call(t, null);
                    }
                }, t.inWitchComponent);
                break;

              case "[object Function]":
                wx.canvasToTempFilePath({
                    canvasId: o,
                    x: u + 4,
                    y: d + 4,
                    width: h - 8,
                    height: s - 8,
                    destWidth: (h - 8) / n,
                    destHeight: (s - 8) / n,
                    success: function(o) {
                        console.log("wx.canvasToTempfilePath res: ", o), e(c) && c.call(t, o.tempFilePath);
                    },
                    fail: function(o) {
                        e(c) && c.call(t, null);
                    }
                }, t.inWitchComponent);
            }
            return t;
        };
    }, f.prototype.cutt = function() {
        var t = this, e = t.width, o = t.height, n = t.cut, i = n.x;
        void 0 === i && (i = 0);
        var r = n.y;
        void 0 === r && (r = 0);
        var a = n.width;
        void 0 === a && (a = e);
        var c = n.height;
        void 0 === c && (c = o), t.outsideBound = function(e, o) {
            t.imgLeft = e >= i ? i : t.scaleWidth + e - i <= a ? i + a - t.scaleWidth : e, t.imgTop = o >= r ? r : t.scaleHeight + o - r <= c ? r + c - t.scaleHeight : o;
        }, t.setBoundStyle = function(n) {
            void 0 === n && (n = {});
            var u = n.color;
            void 0 === u && (u = "#04b00f");
            var d = n.mask;
            void 0 === d && (d = "rgba(0, 0, 0, .5)");
            var h = n.lineWidth;
            void 0 === h && (h = 0), n.x, n.y, n.x, n.y, n.x, n.y, n.x, n.y, n.x, n.y, n.x, 
            n.y, n.x, n.height, n.y, n.x, n.height, n.y, n.x, n.height, n.y, n.x, n.height, 
            n.y, n.x, n.height, n.y, n.x, n.height, n.y, t.ctx.beginPath(), t.ctx.setFillStyle(d), 
            t.ctx.fillRect(0, 0, i, o), t.ctx.fillRect(i, 0, a, r), t.ctx.fillRect(i, r + c, a, o - r - c), 
            t.ctx.fillRect(i + a, 0, e - i - a, o), t.ctx.fill(), t.ctx.setStrokeStyle("#09bb07"), 
            t.ctx.setLineWidth(1), t.ctx.moveTo(n.x, n.y), t.ctx.rect(n.x, n.y, n.width, n.height), 
            t.ctx.stroke();
        };
    }, f.prototype.update = function() {
        var t = this;
        t.src && (t.__oneTouchStart = function(e) {
            t.touchX0 = Math.round(e.x), t.touchY0 = Math.round(e.y);
        }, t.__oneTouchMove = function(e) {
            var o, n;
            if (t.touchended) return t.updateCanvas();
            o = Math.round(e.x - t.touchX0), n = Math.round(e.y - t.touchY0);
            var i = Math.round(t.rectX + o), r = Math.round(t.rectY + n);
            t.outsideBound(i, r), t.updateCanvas();
        }, t.__twoTouchStart = function(e, o) {
            var n, i, r;
            t.touchX1 = Math.round(t.rectX + t.scaleWidth / 2), t.touchY1 = Math.round(t.rectY + t.scaleHeight / 2), 
            n = Math.round(o.x - e.x), i = Math.round(o.y - e.y), r = Math.round(Math.sqrt(n * n + i * i)), 
            t.oldDistance = r;
        }, t.__twoTouchMove = function(e, o) {
            var n = t.oldScale, i = t.oldDistance, r = t.scale, a = t.zoom;
            t.newScale = s(n, i, a, e, o), t.newScale <= 1 && (t.newScale = 1), t.newScale >= r && (t.newScale = r), 
            t.scaleWidth = Math.round(t.newScale * t.baseWidth), t.scaleHeight = Math.round(t.newScale * t.baseHeight);
            var c = Math.round(t.touchX1 - t.scaleWidth / 2), u = Math.round(t.touchY1 - t.scaleHeight / 2);
            t.outsideBound(c, u), t.updateCanvas();
        }, t.__xtouchEnd = function() {
            t.oldScale = t.newScale, t.rectX = t.imgLeft, t.rectY = t.imgTop;
        });
    }, f;
});